import java.io.*;
import java.util.Scanner;

/*
 * Bank.java
 * Class mo ta cho nha bank
 */

public class Bank {
    
   static  int  lastAccountNumber=0;        //vi tri  cua acc tao cuoi cung
   static String fileName = "Accounts.bin"; //ten file chua cac thong tin tai khoan
   Account[] accounts;                      //mang chua cac doi tuong lop Account
   
   Scanner in = new Scanner(System.in);
    /** Creates a new instance of Bank */
   
   //-------------------------------------------------------------------
   //                   Constructor Bank
   //*Khoi tao danh sach acc trong ngan hang la 100 acc
   //*Mo file accounts.bin 
   //*File accounts.bin xem nhu la 1 database luu tru cac acc co trong ngan hang 
   //*Chep cac object acc trong file nay vao mang danh sach acc moi tao 
   // bang cach tao doi tuong cua lop ObjectInputStream
   //
   //*--------------------------------------------------------------------
   
   public Bank() { 
       
      accounts = new Account[100];    //khoi tao 100 tai khoan
      File f = new File(fileName); 
      if(!f.exists()) return;
      try
      {
        FileInputStream fStr = new FileInputStream(f);
        ObjectInputStream oStr = new ObjectInputStream(fStr); //doc object Account tu file Accounts.bin
        Account acc = null ;    //bien trung gian lay object Acc tu file Accounts.bin
        while((acc = (Account)oStr.readObject()) != null )   //khi bien acc duoc lay tu file Account.bin ko null
        {
            accounts[lastAccountNumber++] = acc ;   //chep vao mang accounts
            
        }        
          oStr.close();
          fStr.close();
      }    
     catch(Exception e)
     {   
        // e.printStackTrace();
     }
    
     System.out.println("Tong so Acc khoi tao:" + lastAccountNumber);
   
 }
   //-------------------------------------------------------------------
   //                   creatAccount
   //*Nhap thong tin Account : Ten, Ho,So tien. So tai khoan tu tang len 1 
   //*Tao 1 acc moi chep vao mang chua cac object acc
   //*Tien hanh ghi lai mang nay vao file accounts.bin
   //
   //*--------------------------------------------------------------------
   
   public void creatAccount() // tao 1 tai khoan
   {
        if(lastAccountNumber >100){
           System.out.println("Danh sach full"); 
        }
        String fName="", lName="";
        double balance=0;
     //----------------------nhap ten----------------------------------//  
     do{
        System.out.print("Nhap ten cua ban : ");
        fName = in.next();
        if(fName.equals("")) System.out.println("Ten ko duoc de trong");
     }while(fName.equals(""));
     
    //-------------------------------nhap ho------------------------------//  
      do{
        System.out.print("Nhap Ho cua ban : ");
        lName = in.next();
        if(lName.equals("")) System.out.println("Ho ko duoc de trong");
     }while(lName.equals(""));  
        
    //-------------------------------nhap balance------------------------------//  

     try{ 
     
     do{
     
          System.out.print("Nhap So tien muon goi : ");
        balance = in.nextDouble();
      
        if(balance <= 100 ) System.out.println("Balance phai lon hon 100");
     }while(lName.equals("")); 
    
    
    
   //---------------------------tao 1 acc moi tu thong tin vua nhap--------------//
        
    Account acc = new Account(lastAccountNumber+1,fName,lName,balance);
    accounts[lastAccountNumber++] = acc ;       //dua acc vao cuoi mang
    System.out.println("Tao tai khoan thanh cong");
  }catch(Exception e){
        System.out.println("Balance phai la so"); 
        in.next();
        return;
    }   
    //--------------tien hanh ghi mang acc vao lai file accounts.bin--------------//
    try{
        FileOutputStream f      = new FileOutputStream(fileName);
        ObjectOutputStream oStr = new ObjectOutputStream(f);
        for(int i=0;i< this.lastAccountNumber;i++)
            oStr.writeObject(accounts[i]);
            f.close();
            oStr.close();
        
    }catch(Exception e){};
    
   }
   
   //------------------tim tai khoan voi so account number da biet-------------------//
   private Account find (int number){
        for(int i=0;i<lastAccountNumber;i++)
       if(accounts[i].getAccountNumber() == number) return accounts[i];
       return null;
   }
    
   //---------------------------------------rut tien -------------------------------//
   public void withdraw(){
   try{
       int accNo;      //so tai khoan se rut
        System.out.print("Nhap So tai khoan se rut :");
        accNo = in.nextInt();
        
        Account acc = find(accNo);
        if(acc == null) System.out.println("Tai khoan nay khong ton tai");
        else
        {
            double amount;  // so tien se rut
          
            System.out.print("Mhap so tien se rut :");
            amount = in.nextDouble();
            if(amount > acc.getAccountBalance()-500) 
                System.out.println("so tien rut sai quy dinh");
            else if (amount <0)
                System.out.println("So tien rut phai lon hon 0");
            else{
                acc.withdraw(amount);
                System.out.println("Rut tien thanh cong .");
            }
        }
     }catch(Exception e){
        System.out.print("Loi nhap so.");
        in.next();              
     }
   }
   
   public void deposit(){
        int accNo;          //so tai khoan
     try{
        System.out.print("Nhap so tai khoan can gui :");
        accNo = in.nextInt();
        Account acc = find(accNo);      //tim  tai khoan
        if(acc == null) System.out.println("Tai khoan khong ton tai");
        else{
            double amount;      //so tien gui
            System.out.println("Nhap so tien can gui : ");
            amount = in.nextDouble();
            if(amount<0)
                   System.out.println("So tien gui phai lon hon 0");
            else{
                acc.deposit(amount);
                System.out.println("Gui tien thanh cong");
            }
            
         }
     }catch(Exception e){
        System.out.println("loi nhap so . Yeu cau nhap lai");
        in.next();
     }
              
   }
   
   //---------------------------xem thong tin tai khoan------------------------//
   public void display(){
       
        int accNo ;
     try{
        System.out.print("Nhap so tai khoan can xem :");
        accNo = in.nextInt();
    
        Account acc = find(accNo);
        if(acc == null) System.out.println("Tai khoan khong ton tai");
        else{
            System.out.println(acc.toString());
        }
     }catch(Exception e){
        System.out.println("Loi nhap so . Yeu cau nhap lai");
           in.next();
     }
   }
}
